package ncku.account.event.old;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Calendar;
import java.util.Date;
import ncku.account.data.*;

//新增transaction的暫時介面
public class AddTransaction {
	public SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	public DateFormat df = DateFormat.getDateInstance();
	public Scanner scanner = new Scanner(System.in);
	
	private String description;
	private int amount = 0;
	private Date date;
	private Category category;
	private SubTransactionList subTransList;  //subTransaction List Object
	
	String cat; 
	
	public AddTransaction() throws Exception{
		subTransList = new SubTransactionList();  //新增Transaction時,就先new a subTrasanction List Object
		getTransactionInfo();
		addTrans();
	}
	
	public void getTransactionInfo() throws Exception{
		String temp = "";
		
		System.out.println("Please input the follow informations:");
		System.out.print("Date:");
		date = df.parse(scanner.next());  //儲存交易日期
		
		System.out.print("Description:");
		description = scanner.next();  //儲存交易內容
		
		listCategory show = new listCategory();
		show.listCategory();
		System.out.print("Choose Category/Add Category:");
		cat = scanner.next();
		category = new Category(cat);
		

		System.out.print("是否新增SubTransaction:(Y/N)");
		temp = scanner.next();
		
		if(temp.equals("Y")){  
			System.out.println("若不輸入SubTransaction資料,請打N");
			getSubTransactioInfo();
		}
		
		System.out.print("Amount:(目前累積$" + Integer.toString(amount) +")");
		amount = Integer.valueOf(scanner.next());  //儲存交易金額integer
	}
	
	public void getSubTransactioInfo(){
		String subDescription;
		int subAmount = 0;
		
		boolean conti = true;
		String temp ="";
		 
		while(true){
			System.out.print("SubTransaction description:(N)");
			temp = scanner.next();
			if(temp.equals("N")){
				break;
			}
			else{
				subDescription = temp;
				while(conti){
					System.out.print("SubTransaction amount:(N)");
					temp = scanner.next();
					if(!temp.equals("N")){
						subAmount = Integer.valueOf(temp);
						conti = false;
					}
					else{
						conti = false;
					}
				}
				//取得description amount等資料,並將資料加入ArrayList SubTransaction中
				subTransList.addSubTrans(subDescription, subAmount);
				UpdateNowTransAmount(subAmount);
				conti = true;
			}
		}
	}
	
	public void addTrans(){
		ArrayList<SubTransaction> subTrans;
		subTrans = GlobalData.checkSumOfAmount(amount,subTransList);
		
		GlobalData.add(date, description, amount, category, subTrans);
		System.out.print("Transaction added successfully");
	}
	
	public void UpdateNowTransAmount(int subAmount){
		amount += subAmount;
	}
}
